
$(function () {
    var request = layui.request;
    map();
    setInterval(() => {
        map();
    }, 5000);
    function map() {
        var myChart = echarts.init(document.getElementById('map_1'));
        // var geoCoordMap = {
        //     '城关镇': [113.677356,35.254039],
        //     '照镜镇': [113.701334,35.299096],
        //     '黄堤镇': [113.557135,35.252602],
        //     '中和镇': [113.610709,35.203643],
        //     '徐营镇': [113.563459,35.157436],
        //     '冯庄镇': [113.654277,35.090757],
        //     '亢村镇': [113.699861,35.127794],
        //     '史庄镇': [113.618321,35.259357],
        //     '位庄乡': [113.659807,35.28366],
        //     '大新庄乡': [113.651479,35.167099],
        //     '太山乡': [113.706885,35.193595],
        //     '西工区管理委员会': [113.605189,35.272224],
        // };
        var geoCoordMap = {
            '城关镇': [113.650356,35.254039],
            '照镜镇': [113.680334,35.299096],
            '黄堤镇': [113.530135,35.262602],
            '中和镇': [113.610709,35.203643],
            '徐营镇': [113.553459,35.157436],
            '冯庄镇': [113.634277,35.090757],
            '亢村镇': [113.699861,35.127794],
            '史庄镇': [113.578321,35.235357],
            '位庄乡': [113.619807,35.28366],
            '大新庄乡': [113.651479,35.187099],
            '太山乡': [113.706885,35.213595],
            '西工区管理委员会': [113.580189,35.272224],
        };
        var geoCoordMap2 = {
            '城关镇': [113.677356,35.254039],
            '照镜镇': [113.701334,35.299096],
            '黄堤镇': [113.550135,35.262602],
            '中和镇': [113.610709,35.183643],
            '徐营镇': [113.573459,35.157436],
            '冯庄镇': [113.654277,35.090757],
            '亢村镇': [113.699861,35.107794],
            '史庄镇': [113.598321,35.235357],
            '位庄乡': [113.639807,35.28366],
            '大新庄乡': [113.651479,35.167099],
            '太山乡': [113.706885,35.193595],
            '西工区管理委员会': [113.595189,35.272224],
        };
        var convertData = function (data,map) {
            var res = [];
            for (var i = 0; i < data.length; i++) {
                var geoCoord = map[data[i].name];
                if (geoCoord) {
                    res.push({
                        name: data[i].name,
                        value: geoCoord.concat(data[i].value)
                    });
                }
            }
            return res;
        };
        var option = {
            // backgroundColor: '#404a59',
            title: {
                text: '获嘉县',
                subtext: '销回概况',
                left: 'center',
                textStyle: {
                    color: '#fff'
                },
            },
            tooltip: {
                trigger: 'item',
                formatter: function (params) {
                    if (typeof (params.value)[2] == "undefined") {
                        return params.seriesName+': ' + params.value;
                    } else {
                        return params.seriesName+': ' + params.value[2];
                    }
                }
            },

            geo: {
                map: 'huojia',
                zoom:1.3,
                label: {
                    show: true,
                    emphasis: {
                        show: false
                    }
                },
                roam: false,//禁止其放大缩小
                itemStyle: {
                    normal: {
                        areaColor: '#4c60ff',
                        borderColor: '#002097'
                    },
                    emphasis: {
                        areaColor: '#293fff'
                    }
                }
            },
            series: [
                {
                    name: '销售量',
                    type: 'scatter',
                    coordinateSystem: 'geo',
                    symbolSize: function (val) {
                        return val[2] / 100;
                    },
                    label: {
                        normal: {
                            formatter: '{b}',
                            position: 'right',
                            show: false
                        },
                        emphasis: {
                            show: true
                        }
                    },
                    itemStyle: {
                        normal: {
                            color: '#ffeb7b'
                        }
                    }
                },
                {
                    name: '回收量',
                    type: 'scatter',
                    coordinateSystem: 'geo',
                    symbolSize: function (val) {
                        return val[2] / 100;
                    },
                    label: {
                        normal: {
                            formatter: '{b}',
                            position: 'right',
                            show: false
                        },
                        emphasis: {
                            show: true
                        }
                    },
                    itemStyle: {
                        normal: {
                            color: '#27d08a'
                        }
                    }
                }
            ]
        };

        $.get("js/areaMap.json", function (geoJson) {
            myChart.hideLoading();
            echarts.registerMap('huojia', geoJson);
            request.post("recycleRecord/regionStastics",{}).then(res=>{
                var saleData = res.data.map(item=>{return {name:item.name, value:item.saleAmount}})
                var recycleData = res.data.map(item=>{return {name:item.name, value:item.recycleAmount}})
                option.series[0].data= convertData(saleData,geoCoordMap);
                option.series[1].data= convertData(recycleData,geoCoordMap2);
                myChart.setOption(option);
             })
             
        })
        window.addEventListener("resize", function () {
            myChart.resize();
        });
    }

    function initOption(){
        
    }

})

